<template>
	<view class="container">
		
		
		<!-- #ifdef MP-WEIXIN -->
		<view class="login-title">登录</view>
		<button v-if="!checked&&!getTell" class="login-btn-box" @click="login()" >
			<view class="login-btn">
				<view class="weixin-ico">
					<image lazy-load="true"  src="../../static/images/weixin.png" class="weixin-ico"></image>
				</view>
				<view>微信登录</view>
			</view>
		</button>
		
		<button v-if="checked&&!getTell" class="login-btn-box"  open-type="getPhoneNumber" @getphonenumber="getPhone">
			<view class="login-btn active">
				<view class="weixin-ico">
					<image lazy-load="true"  src="../../static/images/weixin.png" class="weixin-ico"></image>
				</view>
				<view>微信登录</view>
			</view>
		</button>
		
		
		<button v-if="getTell" open-type="getUserInfo" @getuserinfo="getUser_2" class="login-btn-box">
			<view class="login-btn active">
				<view class="weixin-ico">
					<image lazy-load="true"  src="../../static/images/weixin.png" class="weixin-ico"></image>
				</view>
				<view>获取微信用户信息</view>
			</view>
		</button>
		<!-- #endif -->
		
		<!-- #ifdef H5 -->
		<view class="login-title">
			<image src="../../static/images/touxiang5.png" class="login-header"></image>
		</view>
		<view class="boxboxbox">
			<view class="phone-box">
				<image src="../../static/images/icon9.png" class="icon-9"></image>
				<input type="number" placeholder="请输入手机号" @blur="blurInput" v-model="phone_num" class="phone-number" value="" />
			</view>
			<view class="yanzheng">
				<image src="../../static/images/icon8.png" class="icon-8"></image>
				<input type="text" placeholder="请输验证码" @blur="blurInput" v-model="val_code" class="phone-number" value="" />
				<text class="get-code" @click="getCode">
					<block v-if="!is_get">获取验证码</block>
					<block v-if="is_get">{{count_num}}</block>
				</text>
			</view>
		</view>
		<button type="default" @click="Login_H5" class="login-btn-s">登录</button>
		<!-- #endif -->
		
		
		
		
		
		<view class="not-now" @click="no_login">暂不登录</view>
		<view class="rule-box">
			<view class="warm" v-if="showWarm">请先阅读并且同意协议哦</view>
			<view class="checkbox-rule">
				<view class="checkbox">
					<checkbox class="checkbox" @click="setChecked()" :value="checked" checked="checked" style="transform: scale(0.7);"/>
				</view>
				<view class="agree">
					<text>同意</text>
					<text class="rule-1">《诚信布艺网络服务协议》</text>
					<text>及</text>
					<text class="rule-2">《诚信布艺隐私保护声明》</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// import WXBizDataCrypt from '../../static/js/WXBizDataCrypt.js';
	var WXBizDataCrypt = require('../../static/js/WXBizDataCrypt.js');
	export default {
		data() {
			return {
				"checked":true,
				"showWarm":false,
				"getTell":false,
				"tel":'',
				"iv":'',
				"session_key":'',
				"count_num":60,
				"is_get":false,
				phone_num:'',
				val_code:'',
				t_ime:0,
				page:'',
				i_d:0,
				num:0,
				brand_id:0,
				category_id:0,
				name:''
			};
		},
		onLoad(e) {
			var self = this;
			self.page=e.page;
			if(e.id){
				self.i_d = e.id;
			}
			if(e.num){
				self.num = e.num;
			}
			if(e.name){
				self.name = e.name;
			}
			if(e.brand_id){
				self.brand_id = e.brand_id;
			}
			if(e.category_id){
				self.category_id = e.category_id;
			}
			 self.t_ime = (parseInt(uni.getStorageSync('timer')) || 0) + 1;
			uni.setStorageSync('timer',self.t_ime);
			uni.login({
			  provider: 'weixin',
			  success: function (e) {
				  var code = e.code;
			   self.$request.request({
			   	url:'/wechat/get_openid',
			   	data:{'code':code},
			   	success:function(res){
			   		if(res.code==200){
						self.session_key = res.result.session_key;
			   			uni.setStorageSync('session_key',res.result.session_key);
			   			uni.setStorageSync('openid',res.result.openid);
			   		}else{
			   			uni.showToast({
			   				icon:"none",
			   				title: res.msg,
			   				duration: 2000
			   			})
			   		}
			   	}
			   })
			  }
			});
			// #ifdef H5
			let share_time_line={
				title:self.$request.h5_share_name, // 分享标题
				link: self.$request.h5_share_url+'?page=index', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				imgUrl: self.$request.h5_img_url+'/static/images/logo.png', // 分享图标
			}
			let share_app_message={
				title: self.$request.h5_share_name, // 分享标题
				desc: '我发现了一个很不错的商城，买家具有超大实惠！', // 分享描述
				link: self.$request.h5_share_url+'?page=index', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				imgUrl: self.$request.h5_img_url+'/static/images/logo.png', // 分享图标
			}
			uni.setStorageSync('share_time_line',share_time_line);
			uni.setStorageSync('share_app_message',share_app_message);
			self.$request.wx_config()
			// #endif
		},
		methods:{
			setChecked(){
				this.checked = !this.checked
				if(this.checked){
					this.showWarm = false;
				}
			},
			login(){
				if(!this.checked){
					this.showWarm = true;
				}else{
					
				}
			},
			blurInput(){
				uni.pageScrollTo({
				scrollTop: 0,
				duration: 0
				});
			},
			getCode(){
				var self = this;
				var myreg=/^[1][3-9][0-9]{9}$/;
				if (!myreg.test(self.phone_num)) {
				   uni.showToast({
				   	title:'请输入正确的手机号~',
					icon:"none",
					duration:3000
				   });
				   return false;
				}
				if(self.is_get) return;
				
				uni.showLoading({
					title:'请稍后',
				});
				self.$request.request({
					url:'/web/sms/login_code',
					data:{
						mobile:self.phone_num
					},
					success:function(res){
						uni.hideLoading();
						if(res.code==200){
							self.is_get = true;
							uni.showToast({
								title:'发送成功',
								icon:"none",
								duration:2000				
							});
							var loop = setInterval(function(){
								if(self.count_num>0){
									self.count_num--
								}else{
									self.is_get = false;
									self.count_num=60;
									clearInterval(loop)
								}
							}.bind(this),1000);
						}else{
							uni.showToast({
								title:res.msg,
								icon:"none",
								duration:2000				
							});
						}
					}
				})
			},
			Login_H5(){
				var self = this;
				var userdata = JSON.parse(uni.getStorageSync('userdata'));
				var myreg=/^[1][3-9][0-9]{9}$/;
				if (!myreg.test(self.phone_num)) {
				   uni.showToast({
				   	title:'请输入正确的手机号~',
					icon:"none",
					duration:2000
				   });
				   return false;
				}
				if(!self.val_code){
					uni.showToast({
						title:'请输入验证码~',
						icon:"none",
						duration:2000				
					});
					return false;
				}
				if(!this.checked){
					this.showWarm = true;
					return false;
				}
				self.$request.request({
					url:'/web/sms/login',
					data:{
						mobile:self.phone_num,
						code:self.val_code,
						nickname:userdata.nickname,
						avatar:userdata.avatar,
						openid:userdata.openid,
					},
					success:function(res){
						if(res.code==200){
							uni.setStorageSync('token',res.result.token);
							uni.setStorageSync('user',res.result.user);
							let share_id = uni.getStorageSync('share_id') ? uni.getStorageSync('share_id') : 0;
							if(share_id){
								self.$request.request({
									url:'/user/set_bind',
									data:{
										send_user_id:share_id,
										receive_user_id:res.result.user.id
									},
									success:function(res){
										
									}
								});
							}
							uni.showToast({
								title:'登录成功',
								icon:"none",
								duration:2000				
							});
							setTimeout(function(){
								uni.setStorageSync('timer',0);
								self.backTo()
							},2000);
						}else{
							uni.showToast({
								title:res.msg,
								icon:"none",
								duration:2000				
							});
						}
					}
				})
			},	
			getUser(e){
				var self = this;
				this.$request.request({
					url:'/user/login',
					data:{
						mobile:this.tel,
						openid:uni.getStorageSync('openid'),
						nickname:e.userInfo.nickName,
						avatar:e.userInfo.avatarUrl
					},
					success:function(res){
						if(res.code==200){
							console.log(res.result.user)
							uni.setStorageSync('token',res.result.token);
							uni.setStorageSync('user',res.result.user);
							let share_id = uni.getStorageSync('share_id') ? uni.getStorageSync('share_id') : 0;
							if(share_id){
								self.$request.request({
									url:'/user/set_bind',
									data:{
										send_user_id:share_id,
										receive_user_id:res.result.user.id
									},
									success:function(res){
										
									}
								});
							}
							self.backTo()
						}else{
							
						}
					}
				})
			
			},
			no_login(){
				this.backTo()
			},
			backTo(){
				var self = this;
				switch (self.page){
					case 'index':
					case 'guaguaka':
					case 'main':
					case 'shop':
					uni.switchTab({
					    url: '/pages/'+self.page+'/'+self.page
					});
						break;
					case 'order':
					uni.redirectTo({
					    url: '../'+self.page+'/'+self.page+'?id='+self.i_d+'&num='+self.num
					});
						break;
					case 'video-detail':
						uni.redirectTo({
						    url: '../'+self.page+'/'+self.page+'?id='+self.i_d+'&name='+self.name
						});
							break;
					case 'custom-list':
						uni.redirectTo({
						    url: '../../pagesA/'+self.page+'/'+self.page+'?brand_id='+self.brand_id+'&category_id='+self.category_id
						});
							break;	
					case 'pay':
						uni.redirectTo({
						    url: '../../pagesA/pay/pay?id='+self.i_d
						});
							break;		
					case 'custom-detail':
						uni.redirectTo({
						    url: '../../pagesA/'+self.page+'/'+self.page+'?id='+self.i_d
						});
							break;				
					default:
					console.log('../'+self.page+'/'+self.page+'?id='+self.i_d)
					uni.redirectTo({
					    url: '../'+self.page+'/'+self.page+'?id='+self.i_d
					});
					// uni.navigateTo({
					//     url: '../share-details/share-details?id='+id
					// });
						break;
				}
			},
			getUser_2(e){
				this.getUser(e.detail);
			},
			getUser_1(e){
				this.getUser(e);
			},
			getPhone(e){
				var self = this;
				if(e.detail.encryptedData&&e.detail.iv){	
					self.iv = e.detail.iv;
					var sessionKey = self.session_key;
					var appId = uni.getStorageSync('appid');
					var pc =new WXBizDataCrypt(appId, sessionKey);
					var data = pc.decryptData(e.detail.encryptedData,e.detail.iv)
					self.tel = data.phoneNumber;
					// self.getTell = true;
					uni.getUserInfo({
						provider: 'weixin',
						success:function(e){
							self.getUser_1(e)
						},
						fail(e) {
							self.getTell = true;
						}
					})
				}
				
			}
		}
	}
</script>

<style lang="less">
	@backGround:#ffffff;
	@deepColor:#333333;
	@lightColor:#999999;
	.container{
		width: 100%;
		min-height: 100vh;
		background-color: @backGround;
		position: absolute;
	}
	.login-title{
		text-align: center;
		font-size: 28px;
		font-weight: normal;
		color: @deepColor;
		margin-top: 160upx;
	}
	.login-btn{
		width: 80%;
		margin: 80upx auto 60upx auto;
		display: flex;
		flex-direction: row;
		justify-content: center;
		background-color: #cccccc;
		padding: 30upx 0;
		border-radius: 60upx;
		color: #ffffff;
		.weixin-ico{
			width: 42upx;
			height: 38upx;
			margin-right: 20upx;
			position: relative;
			top: 3upx;
		}
	}
	.login-btn-box{
		width: 80%;
		background-color: transparent;
		padding: 0;
		line-height: normal;
	}
	.login-btn-box:after{
		border: none;
	}
	.active{
		background-color: #09bb07;
	}
	.not-now{
		text-align: center;
		color: @lightColor;
		font-size: 15px;
		margin-top: 50upx;
		text-decoration: underline;
	}
	.rule-box{
		position: fixed;
		left: 0;
		bottom: 0;
		padding: 40upx 50upx;
		.warm{
			color: #ffffff;
			background-color: #ff5959;
			text-align: center;
			font-size: 14px;
			width: 380upx;
		}
		checkbox .uni-checkbox-input{
			width: 100upx;
			height: 100upx;
		}
		.checkbox-rule{
			display: flex;
			flex-direction: row;
		}
		.rule-1,.rule-2{
			color: #7887a5;
		}
		.agree{
			font-size: 9px;
			margin-top: 10upx;
		}
	}
	.login-header{
		width: 132upx;
		height: 132upx;
		border-radius: 132upx;
		margin-bottom: 100upx;
	}
	.boxboxbox{
		width: 600upx;
		margin: 0 auto;
		border: 1px solid #d6d6d6;
		border-radius: 10upx;
		display: flex;
		flex-direction: column;
		.phone-box{
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
			padding: 30upx;
			border-bottom: 1px solid #d6d6d6;
			align-items: center;
			.icon-9{
				width: 38upx;
				height: 38upx;
			}
			.phone-number{
				margin-left: 30upx;
			}
		}
		.yanzheng{
			position: relative;
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
			padding: 30upx;
			align-items: center;
			.icon-8{
				width: 28upx;
				height: 34upx;
			}
			.phone-number{
				margin-left: 30upx;
			}
			.get-code{
				position: absolute;
				right:30upx;
				top:24upx;
				padding: 20upx;
				background-color: #859dc2;
				color: #FFFfff;
				line-height: 12px;
				font-size: 12px;
				
			}
		}
	}
	.login-btn-s{
		width: 600upx;
		background-color: #859dc2;
		color: #FFFFFF;
		margin-top: 40upx;
	}
	
</style>
